<template>
  <div class="login">
    <el-form
      ref="LoginForm"
      :model="LoginForm"
      :rules="rules"
      label-position="top"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="LoginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" props="password">
        <el-input v-model="LoginForm.password" type="password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('LoginForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('LoginForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return {
      LoginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 2,
            max: 12,
            message: '用户名长度在 2 到 12 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '密码长度在 6 到 12 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        // if (valid) {
        //   alert('submit!')
        // } else {
        //   console.log('error submit!!')
        //   return false
        // }
        if (!valid) {
          return false
        }
        // console.log(this.LoginForm)
        axios
          .post(
            'http://localhost:8888/api/private/v1/login',
            this.LoginForm
            // {
            //   username: this.LoginForm.username,
            //   password: this.LoginForm.password
            // }
          )
          .then(res => {
            if (res.data.meta.status === 200) {
              this.$router.push('/home')
              this.$message({
                message: res.data.meta.msg,
                type: 'success',
                duratioan: 800
              })
            } else {
              this.$message({
                message: res.data.meta.msg,
                type: 'error',
                duratioan: 100
              })
            }
          })
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped></style>
